package io.renren.membermanage.service.impl;

import cn.hutool.core.lang.Pair;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.membermanage.bo.KeywordListBO;
import io.renren.membermanage.entity.Keyword;
import io.renren.membermanage.entity.KeywordType;
import io.renren.membermanage.mappper.KeywordMapper;
import io.renren.membermanage.service.KeywordService;
import io.renren.membermanage.vo.KeywordListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class KeywordServiceImpl extends ServiceImpl<KeywordMapper, Keyword> implements KeywordService {

    public List<KeywordListBO> getKeywordList() {
        List<KeywordListVO> keywordListVOS = this.baseMapper.selectKeywordList();
        if (!keywordListVOS.isEmpty()){
            List<KeywordListBO> result = new ArrayList<>();
            Map<Pair<Long, String>, List<KeywordListVO>> collect = keywordListVOS.stream().collect(Collectors.groupingBy(c -> Pair.of(c.getTypeId(), c.getKeywordType())));
            collect.forEach((k, v) -> {
                KeywordListBO p = new KeywordListBO();
                KeywordType p1 = new KeywordType();
                List<Keyword> keywordList = new ArrayList<>();
                p1.setId(k.getKey());
                p1.setKeywordType(k.getValue());
                p.setKeywordType(p1);
                v.forEach(c -> {
                    Keyword p2 = new Keyword();
                    p2.setId(c.getKeywordId());
                    p2.setWordName(c.getWordName());
                    p2.setWordEn(c.getWordEn());
                    keywordList.add(p2);
                });
                p.setKeywordList(keywordList);
                result.add(p);
            });
            return result;
        }
        return null;
    }
}
